package td;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

import ui.TwoTowerView;

public class TwoTower extends Tower {
	/*Single or Double bullet*/
	protected DamageTypes dmgType = DamageTypes.None;

	/*Constructor for the TwoTower*/
	public TwoTower(){
		super();
		this.range = 3;
		this.bullet = new SingleBullet(5, StatusEffects.None);
		this.speed = 1;
		this.cost = 70;
		this.viewable = new TwoTowerView(this);
	}
	
	public TwoTower(Field position) {
		super(position);
		this.range = 3;
		this.bullet = new SingleBullet(5, StatusEffects.None);
		this.speed = 1;
		this.cost = 70;
		this.viewable = new TwoTowerView(this);
	}

	/* A method for shooting a bullet. */
	public void shoot(Enemy target) {
		Random rand = new Random();
		int cut = rand.nextInt();
		if (dmgType == DamageTypes.None) {
			/*10% chance of shooting a CutterBullet when the bullet is Single*/
			if (cut % (50 / this.sliceChance) == 1) {
				CutterBullet cutbullet = new CutterBullet(5, StatusEffects.None);
				target.getHurt(cutbullet);
			} else {
				/* Applying bullet damage. */
				target.getHurt(bullet);
			}
		} else if (dmgType == DamageTypes.Dual) {
			/*10% chance of shooting a CutterBullet for the first bullet*/
			if (cut % (50 / this.sliceChance) == 1) {
				CutterBullet cutbullet = new CutterBullet(5, StatusEffects.None);
				target.getHurt(cutbullet);
			} else {
				/* Applying bullet damage. */
				target.getHurt(bullet);
			}
			/*Randomizing second shoot*/
			boolean searching = true;
			Iterator<Field> it = this.inrange.iterator();
			while(it.hasNext() && searching){
				Field second = it.next();
				if(second.getEnemies().size() > 0){
					int randomenemy = rand.nextInt(second.getEnemies().size());
					cut = rand.nextInt();
					if (cut % (100 / this.sliceChance) == 1) {
						CutterBullet cutbullet = new CutterBullet(5, StatusEffects.None);
						second.getEnemies().get(randomenemy).getHurt(cutbullet);
					} else {
						/* Applying bullet damage. */
						second.getEnemies().get(randomenemy).getHurt(bullet);
					}
					//second.getEnemies().get(randomenemy).getPosition().viewable.update();
					searching = false;
				}
			}		
		}
		target.getPosition().viewable.update();
	}

	/*Applying stone effects*/
	public void upgrade(MagicStones stone, int stoneplace) {
		/*If the slot already contains a stone remove its effect because it will be replaced*/
		if (this.stones[stoneplace] != null) {
			degrade(stoneplace);
		} 
			switch (stone) {
			/*Applying Blue stone effect (+1 range)*/
			case Blue:
				this.stones[stoneplace] = stone;
				this.range += 1;
				/*Recalculating inrange Fields*/
				this.inrange.clear();
				this.inRange(this.position, 1);
				break;
			/*Applying Red stone effect (+1 damage)*/
			case Red:
				this.stones[stoneplace] = stone;
				this.bullet.damage += 1;
				break;
			/*Applying Yellow stone effect (-0.25 speed)*/
			case Yellow:
				this.stones[stoneplace] = stone;
				this.speed -= 0.25;
				break;
			/*Applying Rainbow stone effect (DoT effect)*/
			case Rainbow:
				this.stones[stoneplace] = stone;
				this.dmgType = DamageTypes.Dual;
				break;
			}
		
	}

	/*Removing stone effects*/
	public void degrade(int stoneplace) {
		switch (this.stones[stoneplace]) {
		case Blue:
			this.range -= 1;
			this.inrange.clear();
			this.inRange(this.position, 1);
			break;
		case Red:
			this.bullet.damage -= 1;
			break;
		case Yellow:
			this.speed += 0.25;
			break;
		case Rainbow:
			dmgType = DamageTypes.None;
			break;
		}

	}

	/*Applying fog effect (-1 range)*/
	public void applyFog() {
		this.range -= 1;
		this.fogged = true;
		this.viewable.update();
	}

	/*Removing fog effect*/
	public void removeFog() {
		this.range += 1;
		this.fogged = false;
		this.viewable.update();
	}

}
